

package com.hd.assistant.web.home.module.screen.json;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.alibaba.citrus.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import com.hd.assistant.dal.dataobject.User;
import com.hd.assistant.dal.dataobject.ordermining.ItemCost;
import com.hd.assistant.dal.dataobject.ordermining.SkuCost;
import com.hd.assistant.web.common.base.ordermining.BaseTaobaoOrderModule;
import com.hd.util.CommonUtil;
/**
 *
 * @author Hangding
 */
public class ItemcostUpdate extends BaseTaobaoOrderModule{
    private final static Logger log = LoggerFactory.getLogger(ItemcostUpdate.class);
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private HttpSession session;
    
    
   
    public void execute(
    		@Param("itemId") String itemId,
    		@Param("cost") Double cost,
    		@Param("num") Long num,
    		@Param("details") String details,
    		@Param("batchDetails") String batchDetails,
    		@Param("skuDetails") String skuDetails,
    		Context context, Navigator nav) {
    	try{
    		User u = checkPermision(User.Type_TAOBAO_ORDER, session, context, nav);
    		if(u == null)
    			return;
    		if(batchDetails == null){
	    		if(itemId != null && ((cost != null && cost >0) || (num != null))){
	    			//单个设置
	    			ItemCost update = new ItemCost();
	    			update.setItemId(itemId);
	    			if(cost != null)
	    				update.setCost(cost);
	    			if(num != null)
	    				update.setNum(num);
	    			int count = this.orderMiningManager.update(update);
		    		JSONObject object = new JSONObject();
		    		object.put("success", true);
		    		object.put("modifyCount", count);
		    		context.put("result", object.toJSONString());
	    		}
	    		
	    		if(details != null){
	    			//批量单个设置
	    			int totalcount = 0;
	    			String detail[] = StringUtil.split(details, ';');
	    			if(detail != null ){
	    				for(String item : detail){
	    					if(item == null)
	    						continue;
	    					String ss[] = StringUtil.split(item, ':');
	    					if(ss != null && ss.length == 3){
	    						String itemid = ss[0];
	    						double itemCost = CommonUtil.parseDouble(ss[1]);
	    						long nums = CommonUtil.parseLong(ss[2]);
	    						ItemCost update = new ItemCost();
	    		    			update.setItemId(itemid);
    		    				update.setCost(itemCost);
    		    				update.setNum(nums);
	    						totalcount += this.orderMiningManager.update(update);
	    					}
	    				}
	    				JSONObject object = new JSONObject();
	    	    		object.put("success", totalcount == detail.length);
	    	    		object.put("modifyCount", totalcount);
	    	    		context.put("result", object.toJSONString());
	    			}
	    		}
	    		if(skuDetails!=null && itemId != null){
	    			//批量sku设置
	    			ItemCost itemCost = this.orderMiningManager.getItemCostByItemId(itemId);
	    			if(itemCost == null)
	    				return;
	    			ItemCost update = new ItemCost();
	    			update.setItemId(itemId);
	    			String skus[] = StringUtil.split(skuDetails, ";");
	    			for(String sku:skus){
	    				if(sku == null)
	    					continue;
	    				String ss[] = sku.split(":");
	    				if(ss!=null && ss.length==2){
	    					String skuid = ss[0];
	    					double skuCost = CommonUtil.parseDouble(ss[1]);
	    					if(itemCost.getSkuCost().containsKey(skuid)){
	    						SkuCost skuc = itemCost.getSkuCost().get(skuid);
	    						skuc.setCost(skuCost);
	    						itemCost.getSkuCost().put(skuid, skuc);
	    					}
	    				}
	    			}
	    			itemCost.generateSkusFromMap();
	    			update.setSkuCosts(itemCost.getSkuCosts());
	    			update.setHasSkuCosts(true);
	    			int count = this.orderMiningManager.update(update);
		    		JSONObject object = new JSONObject();
		    		object.put("success", true);
		    		object.put("modifyCount", count);
		    		context.put("result", object.toJSONString());
	    		}
    		}else{
    			//如果是批量按折扣设置
    			if(batchDetails != null){
    				String batchs[] = StringUtil.split(batchDetails, ":");
    				if(batchs != null && batchs.length == 2){
    					String ids = batchs[0];
    					String ratioStr = batchs[1];
    					double ratio = CommonUtil.parseDouble(ratioStr);
    					int count = 0;
    					if(ids.equals("all")){
    						count = this.orderMiningManager.batchUpdateCost(u.getUserId(), null, ratio);
    					}else{
    						String items[] = StringUtil.split(ids, ",");
    						count = this.orderMiningManager.batchUpdateCost(u.getUserId(), items, ratio);
    					}
    					JSONObject object = new JSONObject();
    		    		object.put("success", true);
    		    		object.put("modifyCount", count);
    		    		context.put("result", object.toJSONString());
    				}
				}
    		}
    	}catch(Exception e){
    		log.error("error Itemcosts ",e);
    	}
	    	
    }
	
}
